package com.battery.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.enums.YesOrNoEnum;
import com.battery.common.utils.BatteryJSONResult;
import com.battery.common.utils.Constants;
import com.battery.common.utils.DateUtils;
import com.battery.common.utils.GenerateSeqUtil;
import com.battery.common.utils.ali.AliPayUtils;
import com.battery.common.utils.ali.AliTxEntity;
import com.battery.system.domain.*;
import com.battery.system.domain.BO.WithdrawalBO;
import com.battery.system.domain.VO.WithdrawalVO;
import com.battery.system.mapper.*;
import com.github.binarywang.wxpay.bean.entpay.*;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.battery.system.service.IWithdrawalService;
import com.battery.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.print.DocFlavor;

/**
 * 提现记录Service业务层处理
 *
 * @author battery
 * @date 2021-03-18
 */
@Service
public class WithdrawalServiceImpl implements IWithdrawalService {
    @Autowired
    private WithdrawalMapper withdrawalMapper;


    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private PartnerMapper partnerMapper;


    @Autowired
    private MerchantMapper merchantMapper;


    @Autowired
    private PartnerInstallMapper partnerInstallMapper;


    @Autowired
    private MerchantInstallMapper merchantInstallMapper;


    @Autowired(required = false)
    private WxPayService wxPayService;


    /**
     * 查询提现记录
     *
     * @param id 提现记录ID
     * @return 提现记录
     */
    @Override
    public Withdrawal selectWithdrawalById(String id) {
        return withdrawalMapper.selectWithdrawalById(id);
    }

    /**
     * 查询提现记录列表
     *
     * @param withdrawal 提现记录
     * @return 提现记录
     */
    @Override
    public List<Withdrawal> selectWithdrawalList(Withdrawal withdrawal) {
        return withdrawalMapper.selectWithdrawalList(withdrawal);
    }

    @Override
    public List<Withdrawal> selectWithdrawalList(Withdrawal withdrawal, String end) {
        return withdrawalMapper.selectWithdrawalList(withdrawal, end);
    }

    /**
     * 新增提现记录
     *
     * @param withdrawal 提现记录
     * @return 结果
     */
    @Override
    public int insertWithdrawal(Withdrawal withdrawal) {
        return withdrawalMapper.insertWithdrawal(withdrawal);
    }

    /**
     * 修改提现记录
     *
     * @param withdrawal 提现记录
     * @return 结果
     */
    @Override
    public int updateWithdrawal(Withdrawal withdrawal) {
        return withdrawalMapper.updateWithdrawal(withdrawal);
    }

    /**
     * 删除提现记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWithdrawalByIds(String ids) {
        return withdrawalMapper.deleteWithdrawalByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除提现记录信息
     *
     * @param id 提现记录ID
     * @return 结果
     */
    @Override
    public int deleteWithdrawalById(String id) {
        return withdrawalMapper.deleteWithdrawalById(id);
    }

    /**
     * 展示提現列表
     *
     * @param withdrawalBO
     * @return
     */
    @Override
    public List<WithdrawalVO> selectWithdrawalVOList(WithdrawalBO withdrawalBO) {
        return withdrawalMapper.selectWithdrawalVOList(withdrawalBO);
    }

    /**
     * 提现审核通过，进行打款操作
     *
     * @param txId
     * @param type
     * @return
     */
    @Transactional
    @Override
    public AjaxResult adoptTx(String txId, int type) {
        Withdrawal withdrawal = withdrawalMapper.selectWithdrawalById(txId);
        int accountType = withdrawal.getAccountType();
        // 账号类型 1 平台 2 合作商 3 商户
        BigDecimal cashoutMoney = BigDecimal.ZERO;
        String wxAppOpenid = "";
        String aliAppOpenid = "";
        String encBankNo = "";
        String bankCode = "";
        String encTrueName = "";
        if (accountType == 2) {
            // 合作商
            Partner partner = partnerMapper.selectByPrimaryKey(withdrawal.getAccountId());
            cashoutMoney = partner.getCashoutMoney();
            PartnerInstall partnerInstall = partnerInstallMapper.selectByPrimaryKey(partner.getId());
            if (withdrawal.getIsMinTx() == 1) {
                wxAppOpenid = partnerInstall.getAppOpenid();
            } else {
                wxAppOpenid = partnerInstall.getAppWxid();
            }
            aliAppOpenid = partnerInstall.getAppAliid();
            encBankNo = partnerInstall.getEncBankNo();
            bankCode = partnerInstall.getBankCode();
            encTrueName = partnerInstall.getEncTrueName();
        } else if (accountType == 3) {
            // 商户
            Merchant merchant = merchantMapper.selectByPrimaryKey(withdrawal.getAccountId());
            cashoutMoney = merchant.getCashoutMoney();
            MerchantInstall merchantInstall = merchantInstallMapper.selectByPrimaryKey(merchant.getId());
            if (withdrawal.getIsMinTx() == 1) {
                wxAppOpenid = merchantInstall.getAppOpenid();
            } else {
                wxAppOpenid = merchantInstall.getAppWxid();
            }
            aliAppOpenid = merchantInstall.getAppAliid();
            encBankNo = merchantInstall.getEncBankNo();
            bankCode = merchantInstall.getBankCode();
            encTrueName = merchantInstall.getEncTrueName();
        } else {
            // 平台
            return AjaxResult.error("平台暂时无法提现！");
        }
//        BigDecimal txMoney = withdrawal.getApplyAmount(); // 申请提现金额
//        if (cashoutMoney.subtract(txMoney).compareTo(BigDecimal.ZERO) <= 0) {
//            return AjaxResult.error("账户余额为" + cashoutMoney + "元，超出可提现范围，不可提现！请直接拒绝审核");
//        }
//        BigDecimal surplusCost = cashoutMoney.subtract(txMoney); // 剩余金额
        BigDecimal realTxMoney = withdrawal.getAmount(); // 真正提现金额
        Date date = DateUtils.getNowDate();
        if (type != 4) {
            // 提现方式 1微信、2支付宝 3、银行卡 4 线下打款
            if (type == 1) {
                // 微信提现到零钱
                String partner_trade_no = withdrawal.getPartnerTradeNo();
                if (StringUtils.isBlank(partner_trade_no)) {
                    partner_trade_no = GenerateSeqUtil.generateEndRadomCode() + GenerateSeqUtil.getRandomCode(6);
                    Withdrawal withdrawalUpdate = new Withdrawal();
                    withdrawalUpdate.setId(withdrawal.getId());
                    // 状态1、未审核，2、已通过，3、提现成功，4、提现失败，5、已拒绝
                    withdrawalUpdate.setStatus(2);
                    withdrawalUpdate.setPassTime(date);
                    withdrawalUpdate.setTxRequestTime(date);
                    withdrawalMapper.updateWithdrawal(withdrawalUpdate);
                }
                EntPayRequest request = new EntPayRequest();
                request.setPartnerTradeNo(partner_trade_no);
                request.setOpenid(wxAppOpenid);
//                request.setCheckName("FORCE_CHECK"); // 强校验真实姓名
                request.setCheckName("NO_CHECK"); // 不强校验真实姓名
                request.setReUserName(withdrawal.getTrueName()); // 收款用户姓名
                request.setAmount(realTxMoney.multiply(new BigDecimal(100)).intValue());
                request.setDescription("付款给" + withdrawal.getTrueName());
                request.setSpbillCreateIp("47.118.69.251");
                EntPayResult entPayResult = null;
                try {
                    entPayResult = wxPayService.getEntPayService().entPay(request);
                } catch (WxPayException e) {
                    e.printStackTrace();

                    // 提现失败，提示错误原因

                    // TODO 进行提现失败操作
                    txErr(withdrawal.getId(), accountType);

                    Withdrawal withdrawalUpdate = new Withdrawal();
                    withdrawalUpdate.setId(withdrawal.getId());
                    withdrawalUpdate.setStatus(4); // 提现失败
                    withdrawalUpdate.setIsReturn(YesOrNoEnum.YES.getCode());
                    withdrawalMapper.updateWithdrawal(withdrawalUpdate);

                    return AjaxResult.error("提现到零钱发生异常，异常原因：" + e.getErrCodeDes());
                }
                if ("SUCCESS".equals(entPayResult.getReturnCode())) {
                    if ("SUCCESS".equals(entPayResult.getResultCode())) {
                        String payment_no = entPayResult.getPaymentNo(); // 微信付款单号
                        String payment_time = entPayResult.getPaymentTime();
                        Date complete_date = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, payment_time);

                        Withdrawal withdrawalUpdate = new Withdrawal();
                        withdrawalUpdate.setId(withdrawal.getId());
                        withdrawalUpdate.setStatus(3); // 提现成功
                        withdrawalUpdate.setPaymentNo(payment_no); // 微信付款单号
                        withdrawalUpdate.setTxArrivalTime(complete_date); // 提现到账时间
                        withdrawalMapper.updateWithdrawal(withdrawalUpdate);
                    } else {
                        String err_code = entPayResult.getErrCode();
                        if ("NOTENOUGH".equals(err_code)
                                || "SYSTEMERROR".equals(err_code)
                                || "SIGN_ERROR".equals(err_code)
                                || "FREQ_LIMIT".equals(err_code)
                                || "MONEY_LIMIT".equals(err_code)
                                || "CA_ERROR".equals(err_code)
                                || "V2_ACCOUNT_SIMPLE_BAN".equals(err_code)
                                || "PARAM_IS_NOT_UTF8".equals(err_code)
                                || "SENDNUM_LIMIT".equals(err_code)
                                || "NAME_MISMATCH".equals(err_code)) {
                            // TODO 需要原单号以及原请求参数重试

                            // TODO 进行提现失败操作
                            txErr(withdrawal.getId(), accountType);

                            Withdrawal withdrawalUpdate = new Withdrawal();
                            withdrawalUpdate.setId(withdrawal.getId());
                            // 状态1、未审核，2、已通过，3、提现成功，4、提现失败，5、已拒绝
                            withdrawalUpdate.setStatus(4);
                            withdrawalUpdate.setIsReturn(YesOrNoEnum.YES.getCode());
                            withdrawalMapper.updateWithdrawal(withdrawalUpdate);


                            return AjaxResult.error("提现到零钱失败,错误原因：" + err_code);
                        } else {
                            EntPayQueryResult queryResult = null;
                            try {
                                queryResult = wxPayService.getEntPayService().queryEntPay(partner_trade_no);
                            } catch (WxPayException e) {
                                e.printStackTrace();
                                return AjaxResult.error("提现到零钱发生异常，请联系管理员");
                            }
                            if (queryResult == null) {
                                return AjaxResult.error("提现到零钱发生异常，请联系管理员");
                            }
                            String status = queryResult.getStatus();
                            String payment_no = queryResult.getDetailId();
                            /**
                             * SUCCESS:转账成功
                             *
                             * FAILED:转账失败
                             *
                             * PROCESSING:处理中
                             */
                            if ("SUCCESS".equals(status)) {
                                Withdrawal withdrawalUpdate = new Withdrawal();
                                withdrawalUpdate.setId(withdrawal.getId());
                                withdrawalUpdate.setStatus(3); // 提现成功
                                withdrawalUpdate.setPaymentNo(payment_no); // 微信付款单号
                                withdrawalUpdate.setTxArrivalTime(date); // 提现到账时间
                                withdrawalMapper.updateWithdrawal(withdrawalUpdate);
                            } else if ("PROCESSING".equals(status)) {
                                return AjaxResult.error("微信处理中，可点击同步按钮同步信息");
                            } else {
                                // 提现失败
                                // 银行退票，提现失败
                                String err = queryResult.getReason();

                                // TODO 进行提现失败操作
                                txErr(withdrawal.getId(), accountType);


                                Withdrawal withdrawalUpdate = new Withdrawal();
                                withdrawalUpdate.setId(withdrawal.getId());
                                withdrawalUpdate.setStatus(4); // 提现失败
                                withdrawalUpdate.setIsReturn(YesOrNoEnum.YES.getCode());
                                withdrawalMapper.updateWithdrawal(withdrawalUpdate);


                                return AjaxResult.error("提现到零钱失败,错误原因：" + err);
                            }
                        }
                    }
                }
            } else if (type == 3) {
                // 提现到银行卡
                String partner_trade_no = withdrawal.getPartnerTradeNo();
                // TODO 当状态为FAIL时，存在业务结果未明确的情况，所以如果状态为FAIL，请务必通过查询接口确认此次付款的结果（关注错误码err_code字段）。如果要继续进行这笔付款，请务必用原商户订单号和原参数来重入此接口。
                if (StringUtils.isBlank(partner_trade_no)) {
                    partner_trade_no = GenerateSeqUtil.generateEndRadomCode() + GenerateSeqUtil.getRandomCode(6);
                    Withdrawal withdrawalUpdate = new Withdrawal();
                    withdrawalUpdate.setId(withdrawal.getId());
                    // 状态1、未审核，2、已通过，3、提现成功，4、提现失败，5、已拒绝
                    withdrawalUpdate.setStatus(2);
                    withdrawalUpdate.setPassTime(date);
                    withdrawalUpdate.setTxRequestTime(date);
                    withdrawalMapper.updateWithdrawal(withdrawalUpdate);
                }
                EntPayBankRequest request = new EntPayBankRequest();
                request.setPartnerTradeNo(partner_trade_no);
                request.setEncBankNo(encBankNo);
                request.setEncTrueName(encTrueName);
                request.setBankCode(bankCode);
                request.setAmount(realTxMoney.multiply(new BigDecimal(100)).intValue());
                request.setDescription("付款给" + encTrueName);
                EntPayBankResult entPayBankResult = null;
                try {
                    entPayBankResult = wxPayService.getEntPayService().payBank(request);
                } catch (WxPayException e) {
                    e.printStackTrace();

                    // TODO 进行提现失败操作
                    txErr(withdrawal.getId(), accountType);


                    Withdrawal withdrawalUpdate = new Withdrawal();
                    withdrawalUpdate.setId(withdrawal.getId());
                    withdrawalUpdate.setStatus(4); // 提现失败
                    withdrawalUpdate.setIsReturn(YesOrNoEnum.YES.getCode());
                    withdrawalMapper.updateWithdrawal(withdrawalUpdate);


                    return AjaxResult.error("提现到银行卡发生异常，异常原因：" + e.getErrCodeDes());
                }
                if (entPayBankResult == null) {

                    // TODO 进行提现失败操作
                    txErr(withdrawal.getId(), accountType);

                    Withdrawal withdrawalUpdate = new Withdrawal();
                    withdrawalUpdate.setId(withdrawal.getId());
                    withdrawalUpdate.setStatus(4); // 提现失败
                    withdrawalUpdate.setIsReturn(YesOrNoEnum.YES.getCode());
                    withdrawalMapper.updateWithdrawal(withdrawalUpdate);


                    return AjaxResult.error("提现到银行卡发生异常，请联系管理员");
                }
                if ("SUCCESS".equals(entPayBankResult.getReturnCode())) {
                    if ("SUCCESS".equals(entPayBankResult.getResultCode())) {
                        String payment_no = entPayBankResult.getPaymentNo();
                        Withdrawal withdrawalUpdate = new Withdrawal();
                        withdrawalUpdate.setId(withdrawal.getId());
                        withdrawalUpdate.setStatus(3); // 提现成功
                        withdrawalUpdate.setPaymentNo(payment_no); // 微信付款单号
                        withdrawalUpdate.setTxArrivalTime(date); // 提现到账时间
                        withdrawalMapper.updateWithdrawal(withdrawalUpdate);
                    } else {
                        String err_code = entPayBankResult.getErrCode();
                        if ("INVALID_REQUEST".equals(err_code) || "SYSTEMERROR".equals(err_code) || "FREQUENCY_LIMITED".equals(err_code)) {

                            // TODO 进行提现失败操作
                            txErr(withdrawal.getId(), accountType);


                            Withdrawal withdrawalUpdate = new Withdrawal();
                            withdrawalUpdate.setId(withdrawal.getId());
                            // 状态1、未审核，2、已通过，3、提现成功，4、提现失败，5、已拒绝
                            withdrawalUpdate.setStatus(4);
                            withdrawalUpdate.setIsReturn(YesOrNoEnum.YES.getCode());
                            withdrawalMapper.updateWithdrawal(withdrawalUpdate);


                            return AjaxResult.error("提现到零钱失败,错误原因：" + err_code);
                        } else {
                            // 查询对应的付款订单
                            EntPayBankQueryResult queryResult = null;
                            try {
                                queryResult = wxPayService.getEntPayService().queryPayBank(partner_trade_no);
                            } catch (WxPayException e) {
                                e.printStackTrace();
                                // 查询是否付款成功，出现错误
                                return AjaxResult.error("提现到银行卡发生异常，请联系管理员");
                            }
                            if (queryResult == null) {
                                return AjaxResult.error("提现到银行卡发生异常，请联系管理员");
                            }
                            /**
                             * 	代付订单状态：
                             * PROCESSING（处理中，如有明确失败，则返回额外失败原因；否则没有错误原因）
                             * SUCCESS（付款成功）
                             * FAILED（付款失败,需要替换付款单号重新发起付款）
                             * BANK_FAIL（银行退票，订单状态由付款成功流转至退票,退票时付款金额和手续费会自动退还）
                             */
                            String payment_no = queryResult.getPaymentNo();
                            String status = queryResult.getStatus();
                            if ("PROCESSING".equals(status)) {
                                return AjaxResult.error("微信处理中，可点击同步按钮同步信息");
                            } else if ("FAILED".equals(status) || "BANK_FAIL".equals(status)) {
                                // FAILED（付款失败,需要替换付款单号重新发起付款）
                                // 银行退票，提现失败
                                String err = "";
                                if ("BANK_FAIL".equals(status)) {
                                    err = "银行退票,提现失败";
                                } else {
                                    err = queryResult.getFailReason();
                                }
                                Withdrawal withdrawalUpdate = new Withdrawal();
                                withdrawalUpdate.setId(withdrawal.getId());
                                withdrawalUpdate.setStatus(4); // 提现失败
                                withdrawalUpdate.setIsReturn(YesOrNoEnum.YES.getCode());
                                withdrawalMapper.updateWithdrawal(withdrawalUpdate);
                                return AjaxResult.error("提现到银行卡失败,错误原因：" + err);
                            } else if ("SUCCESS".equals(status)) {
                                Withdrawal withdrawalUpdate = new Withdrawal();
                                withdrawalUpdate.setId(withdrawal.getId());
                                withdrawalUpdate.setStatus(3); // 提现成功
                                withdrawalUpdate.setPaymentNo(payment_no); // 微信付款单号
                                withdrawalUpdate.setTxArrivalTime(date); // 提现到账时间
                                withdrawalMapper.updateWithdrawal(withdrawalUpdate);
                            }
                        }
                    }
                }
            } else if (type == 2) {
                // 提现到支付宝
                String out_biz_no = withdrawal.getPartnerTradeNo();
                if (StringUtils.isBlank(out_biz_no)) {
                    out_biz_no = GenerateSeqUtil.generateEndRadomCode() + GenerateSeqUtil.getRandomCode(6);
                    Withdrawal withdrawalUpdate = new Withdrawal();
                    withdrawalUpdate.setId(withdrawal.getId());
                    // 状态1、未审核，2、已通过，3、提现成功，4、提现失败，5、已拒绝
                    withdrawalUpdate.setStatus(2);
                    withdrawalUpdate.setPassTime(date);
                    withdrawalUpdate.setTxRequestTime(date);
                    withdrawalMapper.updateWithdrawal(withdrawalUpdate);
                }
                AliTxEntity txEntity = new AliTxEntity();
                txEntity.setAppid(Constants.tx_appid);
                txEntity.setAppPrivateKey(Constants.tx_appPrivateKey);
                txEntity.setSignType(Constants.tx_signType);
                txEntity.setAppCertPath(Constants.tx_appCertPath);
                txEntity.setAlipayCertPath(Constants.tx_alipayCertPath);
                txEntity.setAlipayRootCertPath(Constants.tx_alipayRootCertPath);
                BatteryJSONResult result = AliPayUtils.aliTx(txEntity, withdrawal.getTrueName(), aliAppOpenid, out_biz_no, realTxMoney, "提现给" + withdrawal.getTrueName());
                if (result.getCode() == 0) {
                    // 支付宝提现失败
                    String err = result.getMsg();

                    // TODO 进行提现失败操作
                    txErr(withdrawal.getId(), accountType);

                    Withdrawal withdrawalUpdate = new Withdrawal();
                    withdrawalUpdate.setId(withdrawal.getId());
                    withdrawalUpdate.setStatus(4); // 提现失败
                    withdrawalUpdate.setIsReturn(YesOrNoEnum.YES.getCode());
                    withdrawalMapper.updateWithdrawal(withdrawalUpdate);

                    return AjaxResult.error("提现到银行卡失败,错误原因：" + err);
                }
                // 提现成功
                AlipayFundTransUniTransferResponse response = (AlipayFundTransUniTransferResponse) result.getData();
                String order_id = response.getOrderId();
                String pay_fund_order_id = response.getPayFundOrderId();
                String trans_date = response.getTransDate();
                Date complete_date = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, trans_date);
                Withdrawal withdrawalUpdate = new Withdrawal();
                withdrawalUpdate.setId(withdrawal.getId());
                withdrawalUpdate.setStatus(3); // 提现成功
                withdrawalUpdate.setOrderId(order_id);
                withdrawalUpdate.setPayFundOrderId(pay_fund_order_id);
                withdrawalUpdate.setTxArrivalTime(complete_date); // 提现到账时间
                withdrawalMapper.updateWithdrawal(withdrawalUpdate);
            }
        } else {
            // 线下提现
            Withdrawal withdrawalUpdate = new Withdrawal();
            withdrawalUpdate.setId(withdrawal.getId());
            // 状态1、未审核，2、已通过，3、提现成功，4、提现失败，5、已拒绝
            withdrawalUpdate.setStatus(3);
            withdrawalUpdate.setPassTime(date);
            withdrawalUpdate.setTxArrivalTime(date);
            withdrawalUpdate.setType(type); // 线下打款
            withdrawalMapper.updateWithdrawal(withdrawalUpdate);
        }
//        if (accountType == 2) {
//            Partner partner = partnerMapper.selectByPrimaryKey(withdrawal.getAccountId());
//            Partner partnerUpdate = new Partner();
//            partnerUpdate.setId(partner.getId());
//            partnerUpdate.setCashoutMoney(surplusCost);
//            partnerMapper.updateByPrimaryKeySelective(partnerUpdate);
//        } else if (accountType == 3) {
//            Merchant merchant = merchantMapper.selectByPrimaryKey(withdrawal.getAccountId());
//            Merchant merchantUpdate = new Merchant();
//            merchantUpdate.setId(merchant.getId());
//            merchantUpdate.setCashoutMoney(surplusCost);
//            merchantMapper.updateByPrimaryKeySelective(merchantUpdate);
//        } else if (accountType == 1) {
//            Project project = projectMapper.selectByPrimaryKey(withdrawal.getAccountId());
//            Project projectUpdate = new Project();
//            projectUpdate.setId(project.getId());
//            projectUpdate.setCashoutMoney(surplusCost);
//            projectMapper.updateByPrimaryKeySelective(projectUpdate);
//        }
        return AjaxResult.success("提现成功！");
    }


    public void txErr(String txId, int accountType) {
        Withdrawal withdrawal = withdrawalMapper.selectWithdrawalById(txId);
        if (withdrawal.getIsReturn() == 0) {
            BigDecimal txApplyAmount = withdrawal.getApplyAmount();
            if (accountType == 2) {
                Partner partner = partnerMapper.selectByPrimaryKey(withdrawal.getAccountId());
                Partner partnerUpdate = new Partner();
                partnerUpdate.setId(partner.getId());
                partnerUpdate.setCashoutMoney(partner.getCashoutMoney().add(txApplyAmount));
                partnerMapper.updateByPrimaryKeySelective(partnerUpdate);
            } else if (accountType == 3) {
                Merchant merchant = merchantMapper.selectByPrimaryKey(withdrawal.getAccountId());
                Merchant merchantUpdate = new Merchant();
                merchantUpdate.setId(merchant.getId());
                merchantUpdate.setCashoutMoney(merchant.getCashoutMoney().add(txApplyAmount));
                merchantMapper.updateByPrimaryKeySelective(merchantUpdate);
            } else if (accountType == 1) {
                Project project = projectMapper.selectByPrimaryKey(withdrawal.getAccountId());
                Project projectUpdate = new Project();
                projectUpdate.setId(project.getId());
                projectUpdate.setCashoutMoney(project.getCashoutMoney().add(txApplyAmount));
                projectMapper.updateByPrimaryKeySelective(projectUpdate);
            }
        }
    }

    /**
     * 需要查询该用户是否存在正在提现中的订单
     *
     * @param accountType
     * @param accountId
     * @return
     */
    @Override
    public Withdrawal selectOn(int accountType, String accountId) {
        return withdrawalMapper.selectOn(accountType, accountId);
    }
}
